package com.library.cx.util;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

//sqlSessionFactory -->SqlSession
public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;

    static{

            //使用Mybatis第一步：获取sqlSessionFactory对象
            String resource="Mybatis-config.xml";
             InputStream inputStream= null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);

    }
    //Sqlsession完全包含了面向数据库执行SQL命令所需的所有方法。
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }


}
